# this script creates a 3-D model of mean return vs LL and WL

Pversion = 200
direction ="short"
duration = 5
method = "ATRPct"

table = "orderLogicTest"
groups <- c("DJI_STOCKS", "SMALL_CAPS", "PM_STOCKS", "TECH_STOCKS")
patterns <- c("S_ADX_1060", "S_ADX_4290", "S_GAP_9228", "L_DONCHIAN_17428")
timeframe <- c("IS", "OOS")

for (group in groups) {
  for (pattern in patterns) {
    if (regexpr("^S_", pattern, perl=TRUE)[1] > 0) {
      direction = "short"
    } else {
      direction = "long"
    }
    i <- 1
        
    for (tf in timeframe) {
      if (tf == "IS") {
        tgroup <- group
      } else if (tf == "OOS"){
        tgroup <- paste(group, "OOS", sep="_")
      }
      values <- alterzero(stopDiagram(tgroup, pattern, Pversion, method, direction=direction, duration=duration, table=table))
      expretddev <- (exp(values[,'returnmean']) - 1) / values[,'returnddev']
      values <- cbind(values, expretddev)
      numberoftrades <- values[1,'numberoftrades']
      
      cat ("now print : returnmean of ", tgroup, pattern, "\n")
      png(file=paste(paste(pattern, group, i, sep="_"), "png", sep="."))
      i <- i+1
      print(wireframe(returnmean ~ stoplosslimit * stopwinlimit, 
                      data=values, 
                      scales=list(arrows=FALSE, col="black", x=list(log = 10), y=list(log = 10), tick.number=2), 
                      main=paste("RETURN", tgroup, pattern, sprintf("(%d)",numberoftrades), sep=" "),
                      colorkey=TRUE,
                      drape=TRUE,
                      screen=list(z=45, x=-55),
                      xlim=c(0.1,10),
                      ylim=c(0.1,10), 
                      xlab="LL",
                      ylab="WL",
                      zlab="Return"))
      dev.off()
      
      cat ("now print : RWRD/RISK of ", tgroup, pattern, "\n")
      png(file=paste(paste(pattern, group, i, sep="_"), "png", sep="."))
      i <- i+1
      print(wireframe(expretddev ~ stoplosslimit * stopwinlimit, 
                      data=values, 
                      scales=list(arrows=FALSE, col="black", x=list(log = 10), y=list(log = 10), tick.number=2), 
                      main=paste("RWRD/RISK", tgroup, pattern, sprintf("(%d)",numberoftrades), sep=" "),
                      colorkey=TRUE,
                      drape=TRUE,
                      screen=list(z=45, x=-55),
                      xlim=c(0.1,10),
                      ylim=c(0.1,10),
                      xlab="LL",
                      ylab="WL",
                      zlab="ExpReturnRiskRatio"))
      dev.off()
    }
  }
}
